package com.gx.smart.building.ui.repair.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.building.SpareApiService
import com.gx.smart.building.SysOrganApiService
import com.gx.smart.building.WorkOrderApiService
import com.gx.smart.building.admin.WorkOrderEventOpinionSettingApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.lib.http.api.RetrofitManager
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.gx.smart.lib.http.api.config.ApiConfig
import com.gx.smart.lib.http.api.response.StatusQuantity
import com.gx.smart.lib.http.api.response.WorkOrder
import com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.QueryWorkOrderEventOpinionSettingListResp
import com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.WorkOrderEventOpinionSettingDto
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysOrganDto
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysOrganQueryResponse
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysUserDto
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.WorkOrderResp
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderEventFileDto
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderPreAssetDto
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareListQueryResp
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareDto
import com.orhanobut.logger.Logger
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * @author: xiaosy
 * 时间: 2022/3/22
 * @Description: java类作用描述
 * @Version: 1.0
 */
class RepairViewModel(application: Application) : CommonViewModel(application) {
    private val countStatusQuantity: MutableLiveData<StatusQuantity> = MutableLiveData()
    private val detail: MutableLiveData<WorkOrder> = MutableLiveData()
    private val receiveSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val processSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val backSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val suspendSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val resumeSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val forwardSuccess: MutableLiveData<Boolean> = MutableLiveData()
    private val departments: MutableLiveData<List<SysOrganDto>> = MutableLiveData()
    private val persons: MutableLiveData<List<SysUserDto>> = MutableLiveData()
    private val pareStockList: MutableLiveData<List<SpareDto>> = MutableLiveData()
    private val replyList: MutableLiveData<List<WorkOrderEventOpinionSettingDto>> =
        MutableLiveData()

    /**
     *查询工单列表
     */
    fun queryWorkOrderList(currentPage: Int, status: List<String>): LiveData<List<WorkOrder>> {
        val orderList: MutableLiveData<List<WorkOrder>> = MutableLiveData()
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()

        RetrofitManager.getInstance().workOrderApiService.queryAppWorkOrderList().enqueue(object: Callback<com.gx.smart.lib.http.api.response.QueryWorkOrderListResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.QueryWorkOrderListResp>,
                response: Response<com.gx.smart.lib.http.api.response.QueryWorkOrderListResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    orderList.value = result.workOrderDto
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.QueryWorkOrderListResp>,
                t: Throwable
            ) {
                Logger.e(t,"error")
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return orderList
    }

    /**
     * countStatusQuantity
     */
    fun countStatusQuantity(): LiveData<StatusQuantity> {

        RetrofitManager.getInstance().workOrderApiService.countAppStatusQuantity().enqueue(object : Callback<com.gx.smart.lib.http.api.response.CountStatusQuantityResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.CountStatusQuantityResp>,
                response: Response<com.gx.smart.lib.http.api.response.CountStatusQuantityResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    countStatusQuantity.value = result.statusQuantity
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.CountStatusQuantityResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return countStatusQuantity
    }

    /**
     * 查看工单详情
     */
    fun queryWorkOrderDetail(id: String): LiveData<WorkOrder> {
        RetrofitManager.getInstance().workOrderApiService.queryAppWorkOrderDetail().enqueue(object :Callback<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp>,
                response: Response<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    detail.value = result.workOrderDto
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.QueryWorkOrderDetailResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return detail
    }

    //接单
    fun receiveTask(
        orderId: String,
        opinion: String
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .receiveTask(orderId, opinion, object : CallBack<WorkOrderResp>() {
                override fun callBack(result: WorkOrderResp?) {
                    if (result == null) {
                        receiveSuccess.value = false
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        receiveSuccess.value = true
                    } else {
                        receiveSuccess.value = false
                        handleServiceException(comResp)
                    }
                }

            })
        return receiveSuccess
    }

    //回退
    fun backTask(
        orderId: String,
        opinion: String
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .backTask(orderId, opinion, object : CallBack<WorkOrderResp>() {
                override fun callBack(result: WorkOrderResp?) {
                    if (result == null) {
                        backSuccess.value = false
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        backSuccess.value = true
                    } else {
                        backSuccess.value = false
                        handleServiceException(comResp)
                    }
                }

            })
        return backSuccess
    }

    //处理
    fun processTask(
        orderId: String,
        opinion: String,
        files: List<WorkOrderEventFileDto>,
        assetId: String?,
        assetName: String?,
        assetDtos: List<WorkOrderPreAssetDto>?,
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .processTask(
                orderId,
                opinion,
                files,
                assetId,
                assetName,
                assetDtos,
                object : CallBack<WorkOrderResp>() {
                    override fun callBack(result: WorkOrderResp?) {
                        if (result == null) {
                            processSuccess.value = false
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            processSuccess.value = true
                        } else {
                            processSuccess.value = false
                            handleServiceException(comResp)
                        }
                    }

                })
        return processSuccess
    }

    //挂起
    fun suspendTask(
        orderId: String,
        opinion: String,
        time: Long
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .suspendTask(orderId, opinion, time, object : CallBack<WorkOrderResp>() {
                override fun callBack(result: WorkOrderResp?) {
                    if (result == null) {
                        suspendSuccess.value = false
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        suspendSuccess.value = true
                    } else {
                        suspendSuccess.value = false
                        handleServiceException(comResp)
                    }
                }

            })
        return suspendSuccess
    }

    //转派
    fun forwardTask(
        orderId: String,
        opinion: String,
        name: String,
        userId: String
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .forwardTask(orderId, opinion, name, userId, object : CallBack<WorkOrderResp>() {
                override fun callBack(result: WorkOrderResp?) {
                    if (result == null) {
                        forwardSuccess.value = false
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        forwardSuccess.value = true
                    } else {
                        forwardSuccess.value = false
                        handleServiceException(comResp)
                    }
                }
            })
        return forwardSuccess
    }

    //解挂
    fun resumeTask(
        orderId: String,
        opinion: String
    ): LiveData<Boolean> {
        WorkOrderApiService.getInstance()
            .resumeTask(orderId, opinion, object : CallBack<WorkOrderResp>() {
                override fun callBack(result: WorkOrderResp?) {
                    if (result == null) {
                        resumeSuccess.value = false
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        resumeSuccess.value = true
                    } else {
                        resumeSuccess.value = false
                        handleServiceException(comResp)
                    }
                }

            })
        return resumeSuccess
    }

    //查询
    fun sysOrganQuery(): LiveData<List<SysOrganDto>> {
        SysOrganApiService.getInstance().sysOrganQuery(object : CallBack<SysOrganQueryResponse>() {
            override fun callBack(result: SysOrganQueryResponse?) {
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    departments.value = result.contentList
                } else {
                    handleServiceException(comResp)
                }
            }
        })
        return departments
    }

    //查询
    fun sysUserQuery(organId: String): LiveData<List<SysUserDto>> {
//        SysUserApiService.getInstance()
//            .sysUserQuery(listOf(organId), object : CallBack<SysUserQueryResponse>() {
//                override fun callBack(result: SysUserQueryResponse?) {
//                    if (result == null) {
//                        ToastUtils.showLong("服务器超时")
//                        return
//                    }
//                    val comResp = result.comResp
//                    if (comResp.code == 100) {
//                        persons.value = result.contentList
//                    } else {
//                        handleServiceException(comResp)
//                    }
//                }
//            })
        return persons
    }

    //查询
    fun sysUserQueryByName(name: String): LiveData<List<SysUserDto>> {
//        SysUserApiService.getInstance()
//            .sysUserQuery(name, object : CallBack<SysUserQueryResponse>() {
//                override fun callBack(result: SysUserQueryResponse?) {
//                    if (result == null) {
//                        ToastUtils.showLong("服务器超时")
//                        return
//                    }
//                    val comResp = result.comResp
//                    if (comResp.code == 100) {
//                        persons.value = result.contentList
//                    } else {
//                        handleServiceException(comResp)
//                    }
//                }
//            })
        return persons
    }

    fun querySpareList(): LiveData<List<SpareDto>> {
        SpareApiService.getInstance()
            .querySpareList(object : CallBack<SpareListQueryResp>() {
                override fun callBack(result: SpareListQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        pareStockList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return pareStockList
    }

    fun querySpareList(keyword: String): LiveData<List<SpareDto>> {
        SpareApiService.getInstance()
            .querySpareList(
                keyword, object : CallBack<SpareListQueryResp>() {
                    override fun callBack(result: SpareListQueryResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            pareStockList.value = result.contentList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return pareStockList
    }

    //查询工单快捷回复列表
    fun queryWorkOrderEventOpinionSettingList(eventCode: String): LiveData<List<WorkOrderEventOpinionSettingDto>> {
        WorkOrderEventOpinionSettingApiService.getInstance()
            .queryWorkOrderEventOpinionSettingList(eventCode,
                object : CallBack<QueryWorkOrderEventOpinionSettingListResp>() {
                    override fun callBack(result: QueryWorkOrderEventOpinionSettingListResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            replyList.value = result.workOrderEventOpinionSettingDtoList;
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
        return replyList
    }
}